<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 论坛评论接口
 */
class Forumcomment extends Api
{
    //protected $noNeedLogin = ['page'];
    protected $noNeedRight = ['*'];


    //评论提交
    public function add()
    {
        $data = [
            'user_id'=>$this->auth->id,
            'forum_id'=>input('forum_id',0),
            'content'=>input('content',''),
            'parent_id'=>input('parent_id',0),
            'top_parent_id'=>input('top_parent_id',0),
        ];
        if (empty($data['content'])) {
            $this->error('请输入评论内容');
        }

        $comment_model = new \app\api\model\forum\ForumComment();

        $res = $comment_model->save($data);

        $this->success('请求成功',$res);
    }

    //删除评论
    public function del()
    {
        $comment_id = input('comment_id',0);

        $detail = \app\api\model\forum\ForumComment::get($comment_id);
        if (!$detail)$this->error('评论不存在');

        $res = $detail->delete();

        if ($res){
            $this->success('删除成功');
        } else {
            $this->success('删除失败');
        }
    }

    //某个帖子的评论列表
    public function page()
    {
        $forum_id = input('forum_id',0);

        $order = input('order','time');

        if ($order=='hot'){
            $orderBy = 'fc.zan desc';
        } else {
            $orderBy = 'fc.createtime desc';
        }

        $list = \app\api\model\forum\ForumComment::alias('fc')
            ->join('user u', 'u.id=fc.user_id', 'LEFT')
            ->field('fc.*,u.nickname,u.avatar')
            ->where(['fc.forum_id'=>$forum_id,'fc.parent_id'=>0])
            ->order($orderBy)
            ->paginate(10, false, ['query' => request()->get()]);

        foreach ($list as $k=>$v){
            $zan_info = \app\api\model\forum\ForumCommentZan::where(['comment_id'=>$v['comment_id'], 'user_id' => $this->auth->id])->find();

            $list[$k]['is_zan'] = $zan_info?1:0;
            $child = \app\api\model\forum\ForumComment::where(['top_parent_id'=>$v['comment_id']])->order('createtime asc')->select();

            if ($child){
                foreach ($child as $kk=>$vv){
                    $user = \app\admin\model\User::where(['id'=>$vv['user_id']])->field('id,nickname,avatar')->find();

                    $child[$kk]['headimg'] = $user?$user['avatar']:'';
                    $child[$kk]['nickname'] = $user?$user['nickname']:'';
                    $parent = \app\api\model\forum\ForumComment::get($vv['parent_id']);

                    $parent_member =  \app\admin\model\User::where(['id'=>$parent['user_id']])->field('id,nickname,avatar')->find();

                    $child[$kk]['reply_nickname'] = $parent_member?$parent_member['nickname']:'';

                    $child[$kk]['create_time'] = date('Y-m-d H:i:s',$vv['createtime']);

                    $zan_info = \app\api\model\forum\ForumCommentZan::where(['comment_id'=>$vv['comment_id'], 'user_id' => $this->auth->id])->find();

                    $child[$kk]['is_zan'] = $zan_info?1:0;
                }
                $list[$k]['child'] = $child;
            } else {
                $list[$k]['child'] = [];
            }

            //$list[$k]['child'] =  $this->getComments($v['comment_id']);
        }

        $this->success('请求成功',$list);
    }

    public function oldpage()
    {
        $forum_id = input('forum_id',0);

        $order = input('order','time');

        if ($order=='hot'){
            $orderBy = 'fc.zan desc';
        } else {
            $orderBy = 'fc.createtime desc';
        }

        $list = \app\api\model\forum\ForumComment::alias('fc')
            ->join('user u', 'u.id=fc.user_id', 'LEFT')
            ->field('fc.*,u.nickname,u.avatar')
            ->where(['fc.forum_id'=>$forum_id,'fc.parent_id'=>0])
            ->order($orderBy)
            ->paginate(10, false, ['query' => request()->get()]);

        foreach ($list as $k=>$v){
            $zan_info = \app\api\model\forum\ForumCommentZan::where(['comment_id'=>$v['comment_id'], 'user_id' => $this->auth->id])->find();

            $list[$k]['is_zan'] = $zan_info?1:0;
            /*$child = \app\api\model\forum\ForumComment::where(['parent_id'=>$v['comment_id']])->order('createtime asc')->select();

            if ($child){
                foreach ($child as $kk=>$vv){
                    $user = \app\admin\model\User::where(['id'=>$vv['user_id']])->field('id,nickname,avatar')->find();

                    $child[$kk]['headimg'] = $user?$user['avatar']:'';
                    $child[$kk]['nickname'] = $user?$user['nickname']:'';
                    $parent = \app\api\model\forum\ForumComment::get($vv['parent_id']);

                    $parent_member =  \app\admin\model\User::where(['id'=>$parent['user_id']])->field('id,nickname,avatar')->find();

                    $child[$kk]['reply_nickname'] = $parent_member?$parent_member['nickname']:'';

                    $child[$kk]['create_time'] = date('Y-m-d H:i:s',$vv['createtime']);

                    $zan_info = \app\api\model\forum\ForumCommentZan::where(['comment_id'=>$vv['comment_id'], 'user_id' => $this->auth->id])->find();

                    $child[$kk]['is_zan'] = $zan_info?1:0;
                }
                $list[$k]['child'] = $child;
            } else {
                $list[$k]['child'] = [];
            }*/

            $list[$k]['child'] =  $this->getComments($v['comment_id']);
        }

        $this->success('请求成功',$list);
    }

    public function getComments($parent_id = 0) {

        $results = \app\api\model\forum\ForumComment::where(['parent_id'=>$parent_id])->order('createtime asc')->select();

        $comments = []; // 存储评论数组
        foreach ($results as $row) {
            $user = \app\admin\model\User::where(['id'=>$row['user_id']])->field('id,nickname,avatar')->find();
            $parent_member =  \app\admin\model\User::where(['id'=>$row['user_id']])->field('id,nickname,avatar')->find();
            $zan_info = \app\api\model\forum\ForumCommentZan::where(['comment_id'=>$row['comment_id'], 'user_id' => $this->auth->id])->find();

            $comment = [
                'comment_id' => $row['comment_id'],
                'content' => $row['content'],
                'forum_id' => $row['forum_id'],
                'user_id' => $row['user_id'],
                'zan_num' => $row['zan_num'],
                'headimg' => $user?$user['avatar']:'',
                'nickname' => $user?$user['nickname']:'',
                'is_zan' => $zan_info?1:0,
                'reply_nickname' => $parent_member?$parent_member['nickname']:'',
                'createtime' => date('Y-m-d H:i:s',$row['createtime']),
                'replies' => $this->getComments($row['comment_id']) // 递归获取子评论
            ];
            $comments[] = $comment;
        }
        return $comments;
    }


}